﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MultiViewer.Display;

namespace MultiViewer.Master
{
    /// <summary>Interaction logic for DisplayGrid.xaml</summary>
    public partial class DisplayGrid : UserControl
    {
        private MasterConfigurationSection config;
        private DisplayStatusControl[,] displays;

        public DisplayGrid()
        {
            InitializeComponent();
        }

        public MasterConfigurationSection Config
        {
            get 
            { 
                return config; 
            }
            set 
            { 
                config = value;
                RebuildGrid();
            }
        }

        private void RebuildGrid()
        {
            displays = new DisplayStatusControl[config.Width, config.Height];
            mainGrid.ColumnDefinitions.Clear();
            for (int i = 0; i < config.Width; i++)
                mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            mainGrid.RowDefinitions.Clear();
            for (int j = 0; j < config.Height; j++)
                mainGrid.RowDefinitions.Add(new RowDefinition());
            for(int i =0;i<config.Width;i++)
                for (int j = 0; j < config.Height; j++)
                {
                    displays[i, j] = new DisplayStatusControl();
                    Grid.SetColumn(displays[i, j], i);
                    Grid.SetRow(displays[i, j], j);
                    mainGrid.Children.Add(displays[i, j]);
                }
        }

        public DisplayStatusControl this[int nodeId]
        {
            get
            {
                return displays[Utils.GetDisplayColumn(nodeId,config.Width,config.Height),
                                Utils.GetDisplayRow(nodeId,config.Width,config.Height)];
            }
        }

        public bool AllNodesAreConnected
        {
            get {
                for (int i = 0; i < config.Width; i++)
                    for (int j = 0; j < config.Height; j++)
                        if (displays[i, j].NodeState != ClientNodeState.Connected)
                            return false;
                return true;
            }
        }
    }
}
